<?php
/*
 * index.action.php     Zhuayi 微博模块
 *
 * @copyright    (C) 2005 - 2010  zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-28
 * @author       zhuayi
 * @QQ			 2179942
 */
class timing_action extends zhuayi
{

	/* 构造函数 */
	function __construct()
	{
		parent::__construct();
				
		$this->db = parent::db();

	}
	
	/* 计划任务首页 */
	function index()
	{
	
	}
	
	/* 读取计划任务 */
	function timing()
	{
		$list = timing_modle::timing();
		/* 写入计划任务 */
		foreach ($list as $val)
		{
			$url = $this->weburl.str_replace('{tid}',$val['id'],str_replace('{aid}',$val['aid'],$val['url']));
			

			$str = timing_modle::task('weibo_'.$val['id'],$url,$val['dtime']);

			echo $str."\r\n";
		}
		
	}
	
	/* 定时微博 */
	function weibo()
	{
		$this->user = member_action::verify();
		$perpage = 20;
		
		$limit = ($_GET['page']-1)*$perpage.','.$perpage;
		
		$list = timing_modle::weibo_list(array('mid'=>$this->user['id']),$limit);
		
		$page=new page(array('total'=>timing_modle::weibo_num(),'perpage'=>$perpage,'ajax'=>'ajax_page','page_css'=>'page_css','pagebarnum'=>5));
		
		require $this->load_tpl('timing_weibo');
	}
	
	/* 查看任务 */
	function look($type,$tid)
	{
		$list = timing_modle::timing_list(array('a.type'=>$type,'a.tid'=>$tid));
		
		require $this->load_tpl('timing_list');
		
	}
	
	/* 修改任务时间 */
	function update($id)
	{
		$info = $this->db->fetch('timing',array('id'=>$id));
		require $this->load_tpl('timing_upload');
	}
	
	/* 修改时间入库 */
	function uplad_info()
	{
		$this->db->update('timing',array('dtime'=>strtotime($_POST['dtime']))," id=".$_POST['id']);
		echo message::ret_json('1','任务时间已经修改完毕！');
	}
	
	/* 删除计划任务 */
	function delete()
	{
		
		$this->db->update('timing',array('del'=>1)," id=".$_POST['id']);
		exit('1');
	}
	
	/* 发布微博 */
	function weibo_save($uid,$aid,$tid)
	{
		/* 判断帐号是否存在 */
		$account = account_modle::account(array('a.id'=>$uid));
		
		if (empty($account['id']))
		{
			exit('不存在此帐号');
		}
		
		/* 查找微博 */
		$body =timing_modle::weibo(array('id'=>$aid));
		
		
		/* 获取微博列表 */
		$app = $account['api'].'_action';
		
		$app = new $app($account['token']);
		
		
		$reset = $app->weibo_info($body['status'],$body['pic']);
		
		if ($reset['error_code'] == '-1')
		{
			$return['position'] = 2;
		}
		else
		{
			$return['position'] = 1;
		}
		
		$return['id'] = $tid;
		
		$return['reset'] = json_encode($reset);
		
		/* 更新任务状态 */
		timing_modle::update($return);
		
		/* 删除计划任务 */
		exec('schtasks /delete /tn weibo_'.$tid.' /f');
		
		exit('<center>发布成功</center>');
		
	}

}
?>